﻿/*!lic_info

The MIT License (MIT)

Copyright (c) 2015 SeaSunOpenSource

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

using System;
using System.Collections.Generic;
using PerfAssist.Common.usmooth.Common;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
#if UNITY_EDITOR

#endif


namespace PerfAssist.Common.usmooth.Runtime.PerfData
{
    public class DataCollector
    {
        public static DataCollector Instance = new DataCollector();

        public static GameObject MainCamera;

        private FrameData _currentFrame;
        private readonly List<FrameData> _frames = new List<FrameData>();

        public FrameData CollectFrameData()
        {
            VisibleMaterials.Clear();
            VisibleTextures.Clear();

            if (MainCamera == null)
            {
                var cam = GameObject.Find("MainCamera");
                if (cam != null) MainCamera = cam;
            }

            //Debug.Log(string.Format("creating frame data. {0}", Time.frameCount));
            _currentFrame = new FrameData();
            _currentFrame._frameCount = Time.frameCount;
            _currentFrame._frameDeltaTime = Time.deltaTime;
            _currentFrame._frameRealTime = Time.realtimeSinceStartup;
            _currentFrame._frameStartTime = Time.time;

            MeshTable.ClearLut();
            var meshRenderers = Object.FindObjectsOfType(typeof(MeshRenderer)) as MeshRenderer[];
            foreach (var mr in meshRenderers)
                if (mr.isVisible)
                {
                    var go = mr.gameObject;
                    if (MeshTable.AddMesh(go))
                    {
                        _currentFrame._frameMeshes.Add(go.GetInstanceID());
                        _nameLut[go.GetInstanceID()] = go.name;

                        //Debug.Log(string.Format("CollectFrameData(): adding game object. {0}, name {1}, name count {2}",
                        //                        go.GetInstanceID(),
                        //                        go.name,
                        //                        _nameLut.Count));

                        foreach (var mat in mr.sharedMaterials)
                        {
                            AddVisibleMaterial(mat, mr.gameObject);

                            if (mat != null)
                            {
#if UNITY_EDITOR
                                if (Application.isEditor)
                                {
                                    var cnt = ShaderUtil.GetPropertyCount(mat.shader);
                                    for (var i = 0; i < cnt; i++)
                                        if (ShaderUtil.GetPropertyType(mat.shader, i) ==
                                            ShaderUtil.ShaderPropertyType.TexEnv)
                                        {
                                            var propName = ShaderUtil.GetPropertyName(mat.shader, i);
                                            AddVisibleTexture(mat.GetTexture(propName), mat);
                                        }
                                }
                                else
#endif
                                {
                                    AddVisibleTexture(mat.mainTexture, mat);
                                }
                            }
                        }
                    }
                }

            _frames.Add(_currentFrame);
            return _currentFrame;
        }

        public void WriteName(int instID, UsCmd cmd)
        {
            string data;
            if (_nameLut.TryGetValue(instID, out data))
            {
                cmd.WriteInt32(instID);
                cmd.WriteStringStripped(data);
            }
        }

        private void AddVisibleMaterial(Material mat, GameObject gameobject)
        {
            if (mat != null)
            {
                if (!VisibleMaterials.ContainsKey(mat)) VisibleMaterials.Add(mat, new HashSet<GameObject>());
                VisibleMaterials[mat].Add(gameobject);
            }
        }

        private void AddVisibleTexture(Texture texture, Material ownerMat)
        {
            if (texture != null)
            {
                if (!VisibleTextures.ContainsKey(texture)) VisibleTextures.Add(texture, new HashSet<Material>());
                VisibleTextures[texture].Add(ownerMat);

                // refresh the size
                if (!_textureSizeLut.ContainsKey(texture))
                    _textureSizeLut[texture] = UsTextureUtil.CalculateTextureSizeBytes(texture);
            }
        }

        public void DumpAllInfo()
        {
            Debug.Log(string.Format("{0} visible materials ({2}), visible textures ({3})",
                DateTime.Now.ToLongTimeString(),
                VisibleMaterials.Count,
                VisibleTextures.Count));

            var matInfo = "";
            foreach (var kv in VisibleMaterials)
                matInfo += string.Format("{0} {1} {2}\n", kv.Key.name, kv.Key.shader.name, kv.Value.Count);
            Debug.Log(matInfo);

            var texInfo = "";
            foreach (var kv in VisibleTextures)
            {
                var tex = kv.Key;
                texInfo += string.Format("{0} {1} {2} {3} {4}\n", tex.name, tex.width, tex.height, kv.Value.Count,
                    UsTextureUtil.FormatSizeString(_textureSizeLut[tex] / 1024));
            }

            Debug.Log(texInfo);
        }

        public UsCmd CreateMaterialCmd()
        {
            var cmd = new UsCmd();
            cmd.WriteNetCmd(eNetCmd.SV_FrameData_Material);
            cmd.WriteInt32(VisibleMaterials.Count);

            foreach (var kv in VisibleMaterials)
            {
                //Debug.Log (string.Format("current_material: {0} - {1} - {2}", kv.Key.GetInstanceID(), kv.Key.name.Length, kv.Key.name));
                cmd.WriteInt32(kv.Key.GetInstanceID());
                cmd.WriteStringStripped(kv.Key.name);
                cmd.WriteStringStripped(kv.Key.shader.name);

                cmd.WriteInt32(kv.Value.Count);
                foreach (var item in kv.Value) cmd.WriteInt32(item.GetInstanceID());
            }

            return cmd;
        }

        public UsCmd CreateTextureCmd()
        {
            var cmd = new UsCmd();
            cmd.WriteNetCmd(eNetCmd.SV_FrameData_Texture);
            cmd.WriteInt32(VisibleTextures.Count);

            foreach (var kv in VisibleTextures)
            {
                cmd.WriteInt32(kv.Key.GetInstanceID());
                cmd.WriteStringStripped(kv.Key.name);
                cmd.WriteString(string.Format("{0}x{1}", kv.Key.width, kv.Key.height));
                cmd.WriteString(UsTextureUtil.FormatSizeString(_textureSizeLut[kv.Key] / 1024));

                cmd.WriteInt32(kv.Value.Count);
                foreach (var item in kv.Value) cmd.WriteInt32(item.GetInstanceID());
            }

            return cmd;
        }

        #region Gathered Meshes/Materials/Textures

        public MeshLut MeshTable {
            get { return new MeshLut();}
        }

        public Dictionary<Material, HashSet<GameObject>> VisibleMaterials {
            get { return new Dictionary<Material, HashSet<GameObject>>();}
        }
            

        public Dictionary<Texture, HashSet<Material>> VisibleTextures { get{return new Dictionary<Texture, HashSet<Material>>();} }
            

        private readonly Dictionary<int, string> _nameLut = new Dictionary<int, string>();
        private readonly Dictionary<Texture, int> _textureSizeLut = new Dictionary<Texture, int>();

        #endregion Gathered Meshes/Materials/Textures
    }
}